﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MDX_Studio_v2
{
	public class PerfmonStats
	{
		private PerfCounter[] _Counters;
		private DateTime _Start;
		private TimeSpan _Duration;
		private Queue<TimeSpan> _Previous = new Queue<TimeSpan>();

		public void Finish()
		{
			foreach (PerfCounter counter in _Counters)
			{
				counter.Finish();
			}
			
			_Duration = (TimeSpan)(DateTime.Now - _Start);
			if (_Duration != null) _Previous.Enqueue(_Duration);
			if (_Previous.Count > 20) _Previous.Dequeue();
		}

		public void Start(string ServerName, string version)
		{
			_Counters = new PerfCounter[]
			{
				//new PerfCounter(ServerName, version, "MDX", "Total calculation covers", "Calc covers      "),
				//new PerfCounter(ServerName, version, "MDX", "Total cells calculated", "Cells calculated "),
				//new PerfCounter(ServerName, version, "MDX", "Total Sonar subcubes", "Sonar subcubes   "),
				///new PerfCounter(ServerName, version, "MDX", "Total NON EMPTY", "NON EMPTYs:        "),
				///new PerfCounter(ServerName, version, "MDX", "Total Autoexist", "Autoexists:        "),
				///new PerfCounter(ServerName, version, "MDX", "Total EXISTING", "EXISTINGs:         "),
				//new PerfCounter(ServerName, version, "Storage Engine Query", "Total measure group queries", "SE queries       "),
				///new PerfCounter(ServerName, version, "MDX", "Total flat cache inserts", "Flat cache insert: "),
				//new PerfCounter(ServerName, version, "Cache", "Total direct hits", "Cache hits       "),
				//new PerfCounter(ServerName, version, "Cache", "Total misses", "Cache misses     "),
				//new PerfCounter(ServerName, version, "Cache", "Total inserts", "Cache inserts    "),
				///new PerfCounter(ServerName, version, "Cache", "Total lookups", "Cache lookups:     "),
				///new PerfCounter(ServerName, version, "Memory", "Data Bytes/sec", "Data Bytes/sec:    ")
				};
			foreach (PerfCounter counter in _Counters)
			{
				counter.Start();
			}
			_Start = DateTime.Now;
		}

		/// <summary>
		/// Gets the Duration in milliseconds
		/// </summary>
		public double Duration
		{
			get { return _Duration.TotalMilliseconds; }
			set { }
		}

		/// <summary>
		/// Gets a list of Previous durations
		/// </summary>
		public TimeSpan[] Previous
		{
			get { return _Previous.Reverse().ToArray(); }	// Reverse Queue and return as a list
			set { }
		}

		public override string ToString()
		{
			//int d = Duration;
			string ds = "";

			if (Duration < 1000) ds = String.Format("{0}ms", Duration);
			else if (Duration < 60000) ds = String.Format("{0}s", Duration / 1000);
			else if (Duration < 3600000) ds = String.Format("{0}m", Duration / 60000);
			else ds = String.Format("{0}h", Duration / 3600000);

			StringBuilder s = new StringBuilder(String.Format("Execution Time:    {0}", ds));

			foreach (PerfCounter p in _Counters)
			{
				s.Append("\r\n" + p.Description + p.Value);
			}

			return s.ToString();
		}
	}
}
